﻿package dropdownmenu{	
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.xml.*;
	import flash.events.*;
	import flash.display.Graphics;
	import flash.display.GradientType;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.display.SimpleButton;
	import gs.TweenLite;
	
	public class DropDownMenu extends Sprite{
		private var menuItem:MenuItem;
		
		private var roll:Boolean=false;
		private var container:Sprite=new Sprite();
		private var req:URLRequest=new URLRequest();
		private var loader:URLLoader=new URLLoader();
		private var txt:TextField=new TextField();
		private var txtFormat:TextFormat=new TextFormat();
		private var button:SimpleButton=new SimpleButton();
		private var _value:String;
		private var _prototype:String;
		private var _container:Sprite;
		private var _itemTextField:TextField;
		private var _defaultFormat:TextFormat = new TextFormat();

		private var _textFieldXPosition:uint = 10;
		private var _textFieldYPosition:uint = 13;
		private var _textFieldWidth:uint = 240;
		private var _textFieldHeight:uint = 25;
		private var _itemPosition:uint = 49;		
		private var _mask:Shape;
		private var _maskWidth:uint =1200;
		private var _maskHeight:uint = 120;
		private var _paddingTop:uint = 120;
		private var _background:Shape;		
		private var _maxSpeed:uint = 15;
		private var _speed:Number;		
		private var _items:XMLList;
		private var strText:String;
		
		
		public function DropDownMenu(url:String=null,_width:int=200,_height:int=30,_text:String="",value:Boolean=false):void{			
			strText=_text;
			container.y=_height;
			container.alpha=0;
			container.visible=false;
			addChild(container);
			var bg:Shape=drawObject(_width,_height);
			addChild(bg);			
			txtFormat.bold=true;
			txtFormat.size=11;
			txtFormat.font="Verdana";
			txt.defaultTextFormat=txtFormat;
			txt.x=5;
			txt.y=3;
			txt.width=_width-10;
			txt.height=_height-6;
			txt.mouseEnabled=false;
			txt.text=strText+" not loaded";
			addChild(txt);			
			button.hitTestState=drawObject(_width,_height);
			button.addEventListener(MouseEvent.CLICK,open);
			addChild(button);
			
			addEventListener(Event.ADDED_TO_STAGE,addedToStage);
			addEventListener(Event.REMOVED_FROM_STAGE,removedFromStage);			
			
				loader.addEventListener(Event.COMPLETE,loaderComplete);
				loader.addEventListener(IOErrorEvent.IO_ERROR,loaderIOError);
				if(url) load(url);
			
			
		}
		
		public function load(url:String):void{
			txt.text="Loading...";
			_value=null;
			var total:int=container.numChildren;
			trace(container.numChildren);
			for(var i:int=0;i<total;i++){ container.removeChildAt(0); }
			req.url=url;
			loader.load(req);
		}
		private function open(e:MouseEvent):void{
			if(container.alpha==0){
				container.visible=true;
				container.alpha=0;
				new TweenLite(container,.5,{alpha:1});
			}
			//else{ close(); }
		}
		private function close():void{ 
			if(container.alpha>0) new TweenLite(container,.5,{alpha:0,onComplete:hideContainer}); 
		}
		private function hideContainer():void{ container.visible=false; }
		private function stageMouseDown(e:MouseEvent):void{ this.close(); }
		private function stageMouseLeave(e:Event):void{ this.close(); }
		private function loaderIOError(e:IOErrorEvent):void{ txt.text=strText+"not found"; }
		private function addedToStage(e:Event):void{ 
			stage.addEventListener(MouseEvent.MOUSE_DOWN,stageMouseDown); 
			stage.addEventListener(Event.MOUSE_LEAVE,stageMouseLeave);
		}
		private function removedFromStage(e:Event):void{ stage.removeEventListener(MouseEvent.MOUSE_DOWN,stageMouseDown); }
		
		//thiet lap gia tri roll
		public function rolL(event:Event=null):void{
				roll=true;
			}
		private function loaderComplete(e:Event):void{
			txt.text="Select "+strText;
			_value=null;
			var xml:XML=XML(e.target.data);
			var total:int=xml.item.length();
			var _w:int=this.width;
			var _h:int=this.height;
			
			
			for(var i=0;i<total;i++){
				var caption:String=xml.item[i].@caption;
				
				var swf:String=xml.item[i].@value;
				var pro:String=xml.item[i].@prototype;
				//if(roll==false){
				menuItem=new MenuItem(caption,swf,pro,_w,_h);
				menuItem.addEventListener(MouseEvent.CLICK,menuItemClick);
				container.addChild(menuItem);
				//}
//				else if(roll== true){
//					
//					}
			
			
			//container.y = _paddingTop;
				
			}
			container.x=(this.width-container.width)*.5;
			_mask = new Shape();
			_mask.graphics.beginFill(0xFF0000);
			_mask.graphics.drawRect(0, 0,_maskWidth,_maskHeight);
			_mask.graphics.endFill();
			_mask.y =80;
			_mask.x=(this.width-container.width)*.5;;
			//_mask.alpha=0;
			
			//addChild(_mask);
			container.mask =_mask;
			container.y=30;
			trace("tim thay "+container.x);
			
			
			
			container.addEventListener(MouseEvent.MOUSE_OVER,mouseOver);
			container.addEventListener(MouseEvent.MOUSE_OUT,mouseOut);
			//addEventListener(Event.ENTER_FRAME, enterFrame);
		}
		private function mouseOver(e:MouseEvent):void{ 
		container.removeEventListener(MouseEvent.MOUSE_OVER, mouseOver);
			
		if (e.target is MenuItem){
				if(container.numChildren>=5)
				addEventListener(Event.ENTER_FRAME, enterFrame);
		
				TweenLite.to(MenuItem(e.target), 1, {alpha:1}); }
			}
		private function mouseOut(e:MouseEvent):void{
			removeEventListener(Event.ENTER_FRAME, enterFrame);
			trace("da remove enterframe");
			container.addEventListener(MouseEvent.MOUSE_OVER, mouseOver);				
			if (e.target is MenuItem) 
				TweenLite.to(MenuItem(e.target),1, {alpha:.8});
			}
			function enterFrame(event:Event):void {
			_speed = ((_mask.height / 2 - _mask.mouseY) / (_mask.height / 2) * _maxSpeed)/2;
			container.y += _speed;
			trace("dat gia tri"+container.y);
			if (container.y >=30) {
				removeEventListener(Event.ENTER_FRAME, enterFrame);
				container.y =30;
			}
			if (container.y <=_mask.height - container.height+30) {
				
				removeEventListener(Event.ENTER_FRAME, enterFrame);
				container.y =_mask.height - container.height+30;
			}
		}
		
			
		private function menuItemClick(e:MouseEvent):void{
			
			if(e.currentTarget.value!=_value && e.currentTarget.caption!=txt.text){
				dispatchEvent(new DropDownMenuEvent(DropDownMenuEvent.CHANGE,e.currentTarget.caption,e.currentTarget.value,e.currentTarget.prototype));
				_value=e.currentTarget.value;
				txt.text=e.currentTarget.caption;
				_prototype=e.currentTarget.prototype;
			}
		}
		private function drawObject(_width:int,_height:int):Shape{
			var newShape=new Shape();
			var colours=[0xA6A6A6,0x515151];
			var alphas=[1,1];
			var ratios=[0,255];
			var radians:Number=90*(Math.PI/180);
			var matrix:Matrix=new Matrix();
			matrix.createGradientBox(_width,_height,radians);
			newShape.graphics.beginGradientFill(GradientType.LINEAR,colours,alphas,ratios,matrix);
			newShape.graphics.drawRoundRect(0,0,_width,_height,5,5);
			return newShape;
		}
		
		public function get caption():String{ return txt.text; }
		public function get value():String{ return _value; }
		public function get prototype():String{ return _prototype; }
	}
}